﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace MDA
{


    /// <summary>
    /// UsersRule 的摘要说明

    /// 用户业务规则
    /// </summary>
    public class UsersRule
    {
        public UsersRule()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 根据用户名密码判断用户是否登录成功

        /// </summary>
        /// <param name="username">用户登录名</param>
        /// <param name="password">用户输入密码</param>
        /// <returns>如果登录成功则返回true，否则返回false</returns>
        public bool ValidUserLoad(string username, string password)
        {
            string sql = "select count (username) from T_user where username=@username and password=@password";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@username", SqlDbType.VarChar);
            com.Parameters.Add("@password", SqlDbType.VarChar);
            com.Parameters["@username"].Value = username;
            com.Parameters["@password"].Value = password;
            DataComman dc = new DataComman();
            object o = dc.ExecuteScalar(com);
            if (o.ToString() == "1")
                return true;
            else return false;
        }


        /// <summary>
        /// 判断某个角色对某个页面是否有访问权限。

        /// </summary>
        /// <param name="rolename">角色名称</param>
        /// <param name="pagesrc">页面路径</param>
        /// <returns>如果有权限，则返回true，否则返回false</returns>
        public bool ValidRolePage(string rolename, string depart, int menu_id)
        {
            string sql = "select count(seq) from T_DEPART_ROLE_MENU where rolename=@rolename and menuid=@menuid";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@rolename", SqlDbType.VarChar);
            com.Parameters.Add("@menuid", SqlDbType.Int);
            com.Parameters["@rolename"].Value = rolename;
            com.Parameters["@menuid"].Value = menu_id;
            DataComman dc = new DataComman();
            object o = dc.ExecuteScalar(com);
            if (o.ToString() == "1")
                return true;
            else return false;
        }

        /// <summary>
        /// 验证用户是否有权限访问某个页面

        /// </summary>
        /// <param name="username">用户名称</param>
        /// <param name="src">页面路径</param>
        /// <returns>如果用户有权限访问指定页面，则返回true，否则返回false</returns>
        //public bool ValidUserPage(string username, string src)
        //{
        //    MenuRule mr = new MenuRule();
        //    int menuid = mr.GetMenu_ID(src);
        //    return ValidUserPage(username, menuid);
        //}

        /// <summary>
        /// 验证用户是否有权限访问某个页面

        /// </summary>
        /// <param name="username">用户名称</param>
        /// <param name="menuid">菜单ID号</param>
        /// <returns></returns>
        public bool ValidUserPage(string username, int menuid)
        {
            string sql = "select count(seq) from T_USER_MENU where username=@username and menuid=@menuid";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@username", SqlDbType.VarChar);
            com.Parameters["@username"].Value = username;
            com.Parameters.Add("@menuid", SqlDbType.VarChar);
            com.Parameters["@menuid"].Value = menuid;
            DataComman dc = new DataComman();
            object o = dc.ExecuteScalar(com);
            if (o.ToString() == "1")
                return true;
            else return false;
        }

        /// <summary>
        /// 根据用户名获取其角色。

        /// </summary>
        /// <param name="username">用户名称</param>
        /// <returns>用户对应的角色名</returns>
        public string GetRoleName(string username)
        {
            string sql = "select Rolename from T_user where username=@username";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@username", SqlDbType.VarChar);
            com.Parameters["@username"].Value = username;
            DataComman dc = new DataComman();
            object o = dc.ExecuteScalar(com);
            return o.ToString();
        }

        /// <summary>
        /// 根据用户名称获得其能够访问的某级子菜单

        /// </summary>
        /// <param name="username">用户名称</param>
        /// <param name="upid">上级菜单ID号</param>
        /// <returns></returns>
        public DataTable GetMenuByUser(string username, int upid)
        {
            T_USERData data = new T_USERData(username);
            T_USERAccess ac = new T_USERAccess();
            ac.Select(data);
            //if (data.HasPopedom == 1)
            //{
            //    return GetMenuByRole(data.DEPARTNAME, data.ROLENAME, upid);
            //}

            string sql = "select t_menu.* from t_menu inner join t_user_menu on (t_menu.menuid=t_user_menu.menuid) where t_menu.upid=@upid and (t_menu.state=1 or t_menu.state=3) and t_user_menu.username=@username order by serial";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@username", SqlDbType.VarChar);
            com.Parameters["@username"].Value = username;
            com.Parameters.Add("@upid", SqlDbType.Int);
            com.Parameters["@upid"].Value = upid;
            DataComman dc = new DataComman();
            DataTable dt = new DataTable();
            dt = dc.SelectCommand(com);
            return dt;

        }

        /// <summary>
        /// 根据用户名称获得其能够访问的某级层次子菜单

        /// </summary>
        /// <param name="username">用户名称</param>
        /// <param name="upid">上级菜单ID号</param>
        /// <returns></returns>
        public DataTable GetLayMenuByUser(string username, int upid)
        {
            T_USERData data = new T_USERData(username);
            T_USERAccess ac = new T_USERAccess();
            ac.Select(data);
            //if (data.HasPopedom == 1)
            //{
            //    return GetMenuByRole(data.DEPARTNAME, data.ROLENAME, upid);
            //}

            string sql = "select t_menu.* from t_menu inner join t_user_menu on (t_menu.menuid=t_user_menu.menuid) where t_menu.upid=@upid and (t_menu.state=1) and t_user_menu.username=@username ORDER BY SERIAL";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@username", SqlDbType.VarChar);
            com.Parameters["@username"].Value = username;
            com.Parameters.Add("@upid", SqlDbType.Int);
            com.Parameters["@upid"].Value = upid;
            DataComman dc = new DataComman();
            DataTable dt = new DataTable();
            dt = dc.SelectCommand(com);
            return dt;

        }

        /// <summary>
        /// 根据角色获得其能够访问的某级菜单。

        /// </summary>
        /// <param name="depart">单位名称</param>
        /// <param name="rolename">角色名称</param>
        /// <param name="upid">上级菜单id</param>
        /// <returns></returns>
        public DataTable GetMenuByRole(string depart, string rolename, int upid)
        {
            string sql = "select t_menu.* from t_menu inner join t_depart_role_menu on(t_menu.menuid=t_depart_role_menu.menuid) where t_depart_role_menu.departname=@departname and t_depart_role_menu.rolename=@rolename and t_menu.upid=@upid";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@departname", SqlDbType.VarChar);
            com.Parameters["@departname"].Value = depart;
            com.Parameters.Add("@rolename", SqlDbType.VarChar);
            com.Parameters["@rolename"].Value = rolename;
            com.Parameters.Add("@upid", SqlDbType.Int);
            com.Parameters["@upid"].Value = upid;
            DataComman dc = new DataComman();
            DataTable dt = new DataTable();
            dt = dc.SelectCommand(com);
            return dt;

        }


        /// <summary>
        /// 判断某用户在某个菜单下面是否有子菜单项。

        /// </summary>
        /// <param name="username">用户名称</param>
        /// <param name="menuid">菜单ID号</param>
        /// <returns>如果有，则返回true。否则返回false</returns>
        public bool HasChildMenu(string username, int menuid)
        {
            string sql = "select count(t_menu.seq) from t_menu inner join T_USER_MENU on (t_menu.menuid=t_user_menu.menuid) where t_menu.upid=@upid and (t_menu.state=1 or t_menu.state=3) and T_USER_MENU.username=@username";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@username", SqlDbType.VarChar);
            com.Parameters["@username"].Value = username;
            com.Parameters.Add("@upid", SqlDbType.VarChar);
            com.Parameters["@upid"].Value = menuid;
            DataComman dc = new DataComman();
            object o = dc.ExecuteScalar(com);
            if ((int)o >= 1)
                return true;
            else return false;
        }

        /// <summary>
        /// 判断某用户在某个菜单下面是否有子菜单项。

        /// </summary>
        /// <param name="username">用户名称</param>
        /// <param name="menuid">菜单ID号</param>
        /// <returns>如果有，则返回true。否则返回false</returns>
        public bool HasLayChildMenu(string username, int menuid)
        {
            string sql = "select count(t_menu.seq) from t_menu inner join T_USER_MENU on (t_menu.menuid=t_user_menu.menuid) where t_menu.upid=@upid and (t_menu.state=1) and T_USER_MENU.username=@username";
            SqlCommand com = new SqlCommand(sql);
            com.Parameters.Add("@username", SqlDbType.VarChar);
            com.Parameters["@username"].Value = username;
            com.Parameters.Add("@upid", SqlDbType.VarChar);
            com.Parameters["@upid"].Value = menuid;
            DataComman dc = new DataComman();
            object o = dc.ExecuteScalar(com);
            if ((int)o >= 1)
                return true;
            else return false;
        }

    }
}